package cn.stylefeng.guns.czlm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.czlm.entity.TbOrder;
import cn.stylefeng.guns.czlm.service.TbOrderService;
import cn.stylefeng.guns.czlm.warpper.TbOrderWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/ord")
@Transactional
public class TbOrderController extends BaseController {

    private static String PREFIX = "/modular/system/order/";

    @Autowired
    private TbOrderService orderservice;

    /**
     * 进入订单详情页
     * @return
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "order.html";
    }


    //查询订单列表
    @RequestMapping("/list")
    @ResponseBody
    public Object findOrders(String condition){
        Page<Map<String, Object>> olist = this.orderservice.selectOrders(condition);
        
        //OrderWrapper数据传到页面上需要进行一次封装
        Page<Map<String, Object>> warpper = new TbOrderWrapper(olist).wrap();
        return LayuiPageFactory.createPageInfo(warpper);
    }

    //跳转到修改页面
    @RequestMapping("/ordPageUpdate")
    public String order_modify_type(@RequestParam("id") Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //获取菜单当前信息，记录日志用
        TbOrder order = this.orderservice.selectById(id);
        LogObjectHolder.me().set(order);
        return PREFIX+"order_modify_type.html";
    }

    //获取回显信息
    @RequestMapping("/getOrdInfo")
    @ResponseBody
    public Object getOrdInfo(@RequestParam Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        TbOrder order = this.orderservice.selectById(id);
        Map<String, Object> map = BeanUtil.beanToMap(order);
        return ResponseData.success(map);
    }

    //修改订单
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData SaveOrdPageUpdate(TbOrder order, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        orderservice.updateOrderById(order);
        return SUCCESS_TIP;
    }

    //发货
    @RequestMapping("/shipment")
    @ResponseBody
    public String shipment(@RequestParam("id") Long id) {
        TbOrder order = orderservice.selectById(id);
        if (order.getOrderStatus()==2) {
            order.setOrderStatus(3);
            orderservice.updateOrderById(order);
            return "true";
        }else {
            return "false";
        }
    }

    //退款
    @RequestMapping("/refund")
    @ResponseBody
    public String refund(@RequestParam("id") Long id) {
        TbOrder order = orderservice.selectById(id);
        if (order.getOrderStatus()==5) {
            order.setOrderStatus(6);
            orderservice.updateOrderById(order);
            return "true";
        }else {
            return "false";
        }
    }






}

